﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  x:Class="System.Windows.Controls.Samples.DatePickerSample">
    <StackPanel>
        <Grid>
            <Grid.Resources>
                <Style x:Key="DefaultStyle" TargetType="controls:DatePicker">
                    <Setter Property="Margin" Value="5"/>
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            
            <TextBlock Grid.Row="0" Grid.Column="0" Text="Selected Date:" />
            <TextBox x:Name="txtSelectedDate" Grid.Row="0" Grid.Column="1" IsReadOnly="true" />
            
            <CheckBox 
              x:Name="chkPastDateSelection"
              Grid.Row="0"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              IsChecked="True"
              Checked="OnPastDatesChanged"
              Unchecked="OnPastDatesChanged"
              Content="Allow the past be selectable"
              Margin="5" />
            
            <controls:DatePicker
              x:Name="sampleDatePicker"
              Grid.Row="1"
              Grid.Column="0"
              Grid.ColumnSpan="4"
              SelectedDateChanged="OnDateSelected"
              Style="{StaticResource DefaultStyle}" />
            
            <controls:DatePicker
              x:Name="sampleLongDatePicker"
              Grid.Row="2"
              Grid.Column="0"
              Grid.ColumnSpan="4"
              SelectedDateFormat="Long"
              SelectedDateChanged="OnLongDateSelected"
              Style="{StaticResource DefaultStyle}" />
        </Grid>
        <src:SourceViewer xmlns:src="clr-namespace:System.Windows.Controls.Samples;assembly=System.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="DatePickerSample.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  x:Class="System.Windows.Controls.Samples.DatePickerSample"&gt;
    &lt;StackPanel&gt;
        &lt;Grid&gt;
            &lt;Grid.Resources&gt;
                &lt;Style x:Key="DefaultStyle" TargetType="controls:DatePicker"&gt;
                    &lt;Setter Property="Margin" Value="5"/&gt;
                    &lt;Setter Property="HorizontalAlignment" Value="Stretch" /&gt;
                &lt;/Style&gt;
            &lt;/Grid.Resources&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="*" /&gt;
                &lt;ColumnDefinition Width="*" /&gt;
                &lt;ColumnDefinition Width="*" /&gt;
                &lt;ColumnDefinition Width="*" /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
            &lt;/Grid.RowDefinitions&gt;
            
            &lt;TextBlock Grid.Row="0" Grid.Column="0" Text="Selected Date:" /&gt;
            &lt;TextBox x:Name="txtSelectedDate" Grid.Row="0" Grid.Column="1" IsReadOnly="true" /&gt;
            
            &lt;CheckBox 
              x:Name="chkPastDateSelection"
              Grid.Row="0"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              IsChecked="True"
              Checked="OnPastDatesChanged"
              Unchecked="OnPastDatesChanged"
              Content="Allow the past be selectable"
              Margin="5" /&gt;
            
            &lt;controls:DatePicker
              x:Name="sampleDatePicker"
              Grid.Row="1"
              Grid.Column="0"
              Grid.ColumnSpan="4"
              SelectedDateChanged="OnDateSelected"
              Style="{StaticResource DefaultStyle}" /&gt;
            
            &lt;controls:DatePicker
              x:Name="sampleLongDatePicker"
              Grid.Row="2"
              Grid.Column="0"
              Grid.ColumnSpan="4"
              SelectedDateFormat="Long"
              SelectedDateChanged="OnLongDateSelected"
              Style="{StaticResource DefaultStyle}" /&gt;
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="DatePickerSample.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;

namespace System.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// Sample page demonstrating the DatePicker.
    /// &lt;/summary&gt;
    [Sample("DatePicker", DifficultyLevel.Basic)]
    [Category("DatePicker")]
    public partial class DatePickerSample : UserControl
    {
        /// &lt;summary&gt;
        /// Flag indicating whether to ignore the next DatePicker update.
        /// &lt;/summary&gt;
        private bool _ignoreNextUpdate = true;

        /// &lt;summary&gt;
        /// Initializes a new instance of the DatePickerSample class.
        /// &lt;/summary&gt;
        public DatePickerSample()
        {
            InitializeComponent();
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Simplifies sample.")]
        private void OnPastDatesChanged(object sender, RoutedEventArgs e)
        {
            if (sampleDatePicker != null &amp;&amp; sampleLongDatePicker != null)
            {
                if (chkPastDateSelection.IsChecked == true)
                {
                    try
                    {
                        sampleDatePicker.BlackoutDates.AddDatesInPast();
                        sampleLongDatePicker.BlackoutDates.AddDatesInPast();
                    }
                    catch
                    {
                        chkPastDateSelection.IsChecked = true;
                    }
                }
                else
                {
                    sampleDatePicker.BlackoutDates.Clear();
                    sampleLongDatePicker.BlackoutDates.Clear();
                }
            }
        }

        /// &lt;summary&gt;
        /// Handle changes to the selected date.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The DatePicker.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        private void OnDateSelected(object sender, SelectionChangedEventArgs e)
        {
            UpdateSelectedDate(sampleDatePicker, sampleLongDatePicker);
        }

        /// &lt;summary&gt;
        /// Handle changes to the selected date.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The long DatePicker.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        private void OnLongDateSelected(object sender, SelectionChangedEventArgs e)
        {
            UpdateSelectedDate(sampleLongDatePicker, sampleDatePicker);
        }

        /// &lt;summary&gt;
        /// Update the selected date.
        /// &lt;/summary&gt;
        /// &lt;param name="current"&gt;The current DatePicker.&lt;/param&gt;
        /// &lt;param name="other"&gt;The other DatePicker.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called via event hanlders attached in XAML.")]
        private void UpdateSelectedDate(DatePicker current, DatePicker other)
        {
            if (_ignoreNextUpdate)
            {
                _ignoreNextUpdate = false;
                other.SelectedDate = current.SelectedDate;

                if (current.SelectedDate != null)
                {
                    txtSelectedDate.Text = current.SelectedDate.ToString();
                }
                else
                {
                    txtSelectedDate.Text = "";
                }
            }
            else
            {
                _ignoreNextUpdate = true;
            }
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="DatePickerSample.xaml.vb">
    <src:SourceFile.Source>
      <sys:String>' (c) Copyright Microsoft Corporation.
' This source is subject to the Microsoft Public License (Ms-PL).
' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
' All other rights reserved.

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Diagnostics.CodeAnalysis
Imports System.Windows
Imports System.Windows.Controls

''' &lt;summary&gt;
''' Sample page demonstrating the DatePicker.
''' &lt;/summary&gt;
&lt;Sample("DatePicker", DifficultyLevel.Basic), Category("DatePicker")&gt; _
Partial Public Class DatePickerSample
    Inherits UserControl
    ''' &lt;summary&gt;
    ''' Flag indicating whether to ignore the next DatePicker update.
    ''' &lt;/summary&gt;
    Private _ignoreNextUpdate As Boolean = True

    ''' &lt;summary&gt;
    ''' Initializes a new instance of the DatePickerSample class.
    ''' &lt;/summary&gt;
    Public Sub New()
        InitializeComponent()
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML."), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification:="Simplifies sample.")&gt; _
    Private Sub OnPastDatesChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If sampleDatePicker IsNot Nothing AndAlso sampleLongDatePicker IsNot Nothing Then
            If chkPastDateSelection.IsChecked = True Then
                Try
                    sampleDatePicker.BlackoutDates.AddDatesInPast()
                    sampleLongDatePicker.BlackoutDates.AddDatesInPast()
                Catch
                    chkPastDateSelection.IsChecked = True
                End Try
            Else
                sampleDatePicker.BlackoutDates.Clear()
                sampleLongDatePicker.BlackoutDates.Clear()
            End If
        End If
    End Sub

    ''' &lt;summary&gt;
    ''' Handle changes to the selected date.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The DatePicker.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML.")&gt; _
    Private Sub OnDateSelected(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        UpdateSelectedDate(sampleDatePicker, sampleLongDatePicker)
    End Sub

    ''' &lt;summary&gt;
    ''' Handle changes to the selected date.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The long DatePicker.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML.")&gt; _
    Private Sub OnLongDateSelected(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        UpdateSelectedDate(sampleLongDatePicker, sampleDatePicker)
    End Sub

    ''' &lt;summary&gt;
    ''' Update the selected date.
    ''' &lt;/summary&gt;
    ''' &lt;param name="current"&gt;The current DatePicker.&lt;/param&gt;
    ''' &lt;param name="other"&gt;The other DatePicker.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Called via event hanlders attached in XAML.")&gt; _
    Private Sub UpdateSelectedDate(ByVal current As DatePicker, ByVal other As DatePicker)
        If _ignoreNextUpdate Then
            _ignoreNextUpdate = False
            other.SelectedDate = current.SelectedDate

            If current.SelectedDate IsNot Nothing Then
                txtSelectedDate.Text = current.SelectedDate.ToString()
            Else
                txtSelectedDate.Text = ""
            End If
        Else
            _ignoreNextUpdate = True
        End If
    End Sub
End Class</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>